// pages/changeaddress/changeaddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    id:"",
    name:"",
    phone:"",
    largeaddress:"",
    detail:"",
    defal:""
  },

  //输入的值发生变化响应e
  all(e){
    //获取数据小标
    let index = wx.getStorageSync('changeNum')
    //修改数据
    this.setData({
      id:index,
      [`${e.currentTarget.dataset.gater}`]: e.detail.value,
    })
  },
  //联动
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      largeaddress: e.detail.value
    })
  },
  //保存地址
  localaddress(){
    //接收点击的下标
    let index = wx.getStorageSync('changeNum')
    //接受地址数组
    let addressData = wx.getStorageSync('chooseaddress')
    //查找点击的下标
    let change = addressData.findIndex(item=>item.id==index)
    //换数据
    addressData = addressData.filter(item=>item.id != index)
    let item = {
      id:this.data.id,
      name:this.data.name,
      phone:this.data.phone,
      largeaddress:this.data.largeaddress,
      detail:this.data.detail,
      defal:this.data.defal
    }
    addressData.splice(change,0,item)
    //保存进本地
    wx.setStorageSync('chooseaddress', addressData)
    // console.log(addressData);
    wx.navigateBack({
      delta:1
    })
  },
  //删除地址
  deladdress(){
    //接收点击的下标
    let index = wx.getStorageSync('changeNum')
    //接受地址数组
    let addressData = wx.getStorageSync('chooseaddress')
    //查找点击的下标
    let change = addressData.findIndex(item=>item.id==index)
    //删除这个数组
    addressData.splice(change,1)
    //保存本地
    wx.setStorageSync('chooseaddress', addressData)
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //接收点击的下标
    let index = wx.getStorageSync('changeNum')
    //接受地址数组
    let addressData = wx.getStorageSync('chooseaddress')
    //查找点击的数组
    let change = addressData.filter(item=>item.id==index)[0]
    //修改data的数据
    this.setData({
      id:change.id,
      name:change.name,
      phone:change.phone,
      region:change.largeaddress,
      largeaddress:change.largeaddress,
      detail:change.detail,
      defal:change.defal,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})